package com.hnchances.controller;


import com.hnchances.common.vo.ResultVo;
import com.hnchances.entity.User;
import com.hnchances.entity.vo.RegisterUser;
import com.hnchances.service.UserService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-08-11
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 用户信息查询

    @ApiOperation("用户信息查询接口")
    @GetMapping("userinfo")
    public ResultVo select(@RequestHeader("token")String token){
        JwtParser parser = Jwts.parser();
        parser.setSigningKey("zhangxueyang");
        Jws<Claims> claimsJws = parser.parseClaimsJws(token);
        Integer id = (Integer) claimsJws.getBody().get("id");
        return userService.select(id);
    }

    // 用户注册

    @ApiOperation("用户注册接口")
    @PostMapping("registered")
    public ResultVo registered(@RequestBody RegisterUser user){
        return  userService.registered(user);
    }

    // 用户登录
    @ApiOperation("用户登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "String",name = "userName",value = "用户登录账号",required = true),
            @ApiImplicitParam(paramType="string",name = "password",value = "用户登录密码",required = true)
    })
    @PostMapping("login")
    public ResultVo loginUser(@RequestParam("userName") String userName,@RequestParam("password")String password){
        return userService.login(userName,password);
    }

    // 用户更新
    @ApiOperation("用户更新接口")
    @PostMapping("update")
    public ResultVo updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    // 用户删除
    @ApiOperation("用户删除")
    @PostMapping("del")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value ="用户ID" ,required = true),
    })
    public ResultVo deleteUser(@RequestParam("userId") Integer userId) {
         return  userService.delete(userId);
    }


}

